import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
// import { DrillingRight } from '@/api/DrillingRight'



const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    userId:"",
    roles:'',
    teamId:'',
    teamName:'',
    post:'',
    centerId:'',
    deptId:'',
    groupId:'',
    checkRoles:null,//点击角色切换选中的视角
    orginIds:{},//原中心部门小组的数据备份
    defaultOrg:'',//原机构id和切换后的id共用一个
    // isDrillingRight:false//是否有权限查看

  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_TEAMID: (state, teamId) => {
    state.teamId = teamId
  },
  SET_TEAMNAME: (state, teamName) => {
    state.teamName = teamName
  },
  SET_POST: (state, post) => {
    state.post = post
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_CENTERID: (state, centerId) => {
    state.centerId = centerId
  },
  SET_DEOTID: (state, deptId) => {
    state.deptId = deptId
  },
  SET_GROUPID: (state, groupId) => {
    state.groupId = groupId
  },
  SET_CHECKROLES: (state, checkRoles) => {
    state.checkRoles = checkRoles
  },
  SET_ORGINIDS: (state, orginIds) => {
    state.orginIds = orginIds
  },
  SET_DEFAULTORG: (state, defaultOrg) => {
    state.defaultOrg = defaultOrg
  },
  ADD_ROLE: (state, role) => {
    if(state.roles.includes(role)) return false;
    state.roles.push(role)
  },
  // SET_ISDRILLINGRIGHT:(state,isDrillingRight)=>{
  //   state.isDrillingRight=isDrillingRight
  // }
}

const actions = {
  changeRole({commit},roles){
    return commit("SET_ROLES",roles)
  },
  addRole({commit},role){
     commit("ADD_ROLE",role)
  },
  // async changeIsDrillingRight({commit},id){
  //   let res=await DrillingRight({DrillingRight:id})
  //   commit('SET_ISDRILLINGRIGHT',res.data)
  //   return res.data
  // },
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit("SET_TOKEN",'')
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit("SET_TOKEN",'')
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

